/**
 * @param {string[]} words
 * @param {string} pattern
 * @return {string[]}
 */
 var findAndReplacePattern = function(words, pattern) {
  const judge = (a, b) => {
      const pat = new Map();
      const used = new Map();
      for(let i = 0; i < a.length; i++) {
          if(!pat.has(b[i])) {
              if(used.has(a[i])) return false;
              pat.set(b[i], a[i]);
              used.set(a[i], true);
          }
          else if(pat.get(b[i])!==a[i]) return false;
      }
      return true;
  }
  return words.filter(e => judge(e, pattern));
};